﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EasyLibrary.AR.Classes
{
  public class GpsCoordinatesSegment
    {
        public GpsCoordinates[] Coordinates { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public int MaxSpeed { get; set; }
        public int AverageSpeed { get; set; }
        public string StartAddress { get; set; }
        public string EndAddress { get; set; }
        /// <summary>
        /// Sum of Distance in Kms
        /// </summary>
        public float Distance { get; set; }
        public double StartFuelLevel { get; set; }
        public double EndFuelLevel { get; set; }
        public double MaxFuelLevel { get; set; }
        public double MinFuelLevel { get; set; }
        public double AverageFuelLevel { get; set; }
        public double Consumption { get; set; }
        public double StartMediaFuelLevel { get; set; }
        public double EndMediaFuelLevel { get; set; }
        public double Kms { get; set; }
        public int IdInicial { get; set; }
        public int IdFinal { get; set; }



        public GpsCoordinatesSegment getSegmentFast(IEnumerable<GpsCoordinates> points, int start, int end)
        {
            var segment = new GpsCoordinatesSegment { Coordinates = points.Skip(start).Take(end - start + 1).ToArray() };

            segment.AverageSpeed = (int)segment.Coordinates.Average(c => c.Speed);
            segment.MaxSpeed = (int)segment.Coordinates.Max(c => c.Speed);
            segment.Distance = segment.Coordinates.Sum(c => c.Distance);
            //segment.StartFuelLevel = (int)segment.Coordinates.Max(c => c.FuelLevel);


            segment.AverageFuelLevel = segment.Coordinates.Average(c => c.FuelLevel);
            var s = segment.Coordinates.Count(c => c.FuelLevel == segment.MaxFuelLevel);

            //if (segment.MaxSpeed <= 0 || segment.Distance <= 0.1) return null;

            var first = segment.Coordinates.First();
            var last = segment.Coordinates.Last();

            double fuelIni = 0;
            double distIni = 0;
            double fuelFim = 0;
            double distFim = 0;


            segment.StartDate = first.Timestamp;
            segment.EndDate = last.Timestamp;


            if ((segment.MaxSpeed < 5 || segment.Distance <= 0.02))
                return null;
            else
                return segment;

        }

    }

}
